package javacore.dtypeopt.operator;

public class OperatorDemo {
	public OperatorDemo() {
		// nothing here
	}

	public void tryArithmeticOperators() {
		/* init 2 example variables */
		int x = 20;
		int y = 100;
		System.out.println("x = " + x + ", y = " + y);

		System.out.println("x + y = " + (x + y));

		x++;
		System.out.println("After x++, x = " + x);

		System.out.println("--x, we receive x = " + --x);
	}

	/* Base on example on the page 40 */
	public void tryRelationalOperators() {
		int i = 9;
		int j = 10;
		if (i < j) {
			System.out.println("i is less than j");
		}
		if (i <= j) {
			System.out.println("i is less than or equal to j");
		}
		if (i >= j) {
			System.out.println("i is greater than or equal to j");
		}
	}

	public void tryLogicalOperators() {
		/* bitwise operators */
		byte x = 117;
		byte y = 89;

		// and
		System.out.println("x&y = " + (x & y));

		// or
		System.out.println("x|y = " + (x | y));

		// xor
		System.out.println("x^y = " + (x ^ y));

		// bitwise inversion
		System.out.println("~x = " + (~x));

		// boolean inversion => x isnt a boolean type
		// System.out.println("!x = " + (!x) );

		/* Short-Circuit Logical Operators on page 44*/
		System.out.println("\nShort-Circuit Logical Operators");
		tryShortCircuitLogicalOperators();
	}

	private void tryShortCircuitLogicalOperators() {
		int i = 5;
		int j = 10;
		int k = 15;
		if ((i < j) || (k++ > j)) {
			System.out.println("First if, value of k: " + k);
		}
		
		if ((i < j) && (k++ < j)) {
			System.out.println("Second if, value of k: " + k);
		}
		
		System.out.println("Out of if, k:" + k);
	}

	public void tryAdvancedOperators(){
		String str1 = "hello world";
		String str2 = new String("hello world");
		
		if (str1.equals(str2)) 
			System.out.println("Yeah, tow string is equal");
		else 
			System.out.println("Boof, why isnt they equal ???");
	}
	
	public static void main(String[] args) {
		OperatorDemo demo = new OperatorDemo();

		System.out.println("Arithmetic Operators Demo");
		demo.tryArithmeticOperators();
		System.out.println();

		System.out.println("Relational Operators Demo");
		demo.tryRelationalOperators();
		System.out.println();

		System.out.println("Logical Operators Demo");
		demo.tryLogicalOperators();
		System.out.println();

		System.out.println("Advanced Operators Demo");
		demo.tryAdvancedOperators();;
		System.out.println();
}
}
